package net.office.controller;

import net.office.bean.BaseDict;
import net.office.bean.StaffInfo;
import net.office.service.BaseDictService;
import net.office.service.StaffInfoService;
import net.office.utils.TemplateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StaffInfoController {

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private BaseDictService baseDictService;

    /*查询列表*/
    @RequestMapping("/staff/all/list.do")
    public String allList(Model model){

        List<StaffInfo> staffInfoList = staffInfoService.findAll();

        for(int i=0; i<staffInfoList.size(); i++){
            if(staffInfoList.get(i).getStaffSex() != null) {
                staffInfoList.get(i).setSexDict(baseDictService.findOneByDictId(staffInfoList.get(i).getStaffSex()));
            }
            if(staffInfoList.get(i).getStaffSex() != null) {
                staffInfoList.get(i).setSexDict(baseDictService.findOneByDictId(staffInfoList.get(i).getStaffSex()));
            }
        }

        model.addAttribute("staffInfoList", staffInfoList);

        List<BaseDict> sexList = baseDictService.findByDictTypeCode(TemplateParam.SEX_CODE);
        List<BaseDict> isWorkList = baseDictService.findByDictTypeCode(TemplateParam.IS_CODE);
        model.addAttribute("sexList", sexList);
        model.addAttribute("isWorkList", isWorkList);

        return "staff";
    }

    /*搜索列表*/
    @RequestMapping("/staff/search.do")
    public String search(@RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffDepartment", required = false) String staffDepartment,
                         @RequestParam(value = "staffPost", required = false) String staffPost,
                         @RequestParam(value = "staffIsWork", required = false) Integer staffIsWork,
                         HttpSession session, Model model){

        StaffInfo staffInfo = new StaffInfo();

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffInfo.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffInfo.setStaffName(staffName);
        }
        if(staffDepartment!=null && staffDepartment.trim().length()!=0){
            staffInfo.setStaffDepartment(staffDepartment);
        }
        if(staffPost!=null && staffPost.trim().length()!=0){
            staffInfo.setStaffPost(staffPost);
        }
        if(staffIsWork != null){
            staffInfo.setStaffIsWork(staffIsWork);
        }

        List<StaffInfo> staffInfoList = staffInfoService.findByStaffInfo(staffInfo);

        for(int i=0; i<staffInfoList.size(); i++){
            if(staffInfoList.get(i).getStaffSex() != null) {
                staffInfoList.get(i).setSexDict(baseDictService.findOneByDictId(staffInfoList.get(i).getStaffSex()));
            }
            if(staffInfoList.get(i).getStaffSex() != null) {
                staffInfoList.get(i).setSexDict(baseDictService.findOneByDictId(staffInfoList.get(i).getStaffSex()));
            }
        }

        model.addAttribute("staffInfoList", staffInfoList);

        List<BaseDict> sexList = baseDictService.findByDictTypeCode(TemplateParam.SEX_CODE);
        List<BaseDict> isWorkList = baseDictService.findByDictTypeCode(TemplateParam.IS_CODE);
        model.addAttribute("sexList", sexList);
        model.addAttribute("isWorkList", isWorkList);

        return "staff";
    }

    /*新增*/
    @RequestMapping("/staff/insert.do")
    public String insert(@RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffDepartment", required = false) String staffDepartment,
                         @RequestParam(value = "staffPost", required = false) String staffPost,
                         @RequestParam(value = "staffSex", required = false) Integer staffSex,
                         @RequestParam(value = "staffBirthday", required = false) Date staffBirthday,
                         @RequestParam(value = "staffIdCard", required = false) String staffIdCard,
                         @RequestParam(value = "staffIsWork", required = false) Integer staffIsWork,
                         @RequestParam(value = "staffPassword", required = false) String staffPassword,

                         HttpSession session){

        StaffInfo staffInfo = new StaffInfo();

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffInfo.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffInfo.setStaffName(staffName);
        }
        if(staffSex != null){
            staffInfo.setStaffSex(staffSex);
        }
        if(staffBirthday != null){
            staffInfo.setStaffBirthday(staffBirthday);
        }
        if(staffDepartment!=null && staffDepartment.trim().length()!=0){
            staffInfo.setStaffDepartment(staffDepartment);
        }
        if(staffPost!=null && staffPost.trim().length()!=0){
            staffInfo.setStaffPost(staffPost);
        }
        if(staffIdCard!=null && staffIdCard.trim().length()!=0){
            staffInfo.setStaffIdCard(staffIdCard);
        }
        if(staffIsWork!=null){
            staffInfo.setStaffIsWork(staffIsWork);
        }
        if(staffPassword!=null && staffPassword.trim().length()!=0){
            staffInfo.setStaffPassword(staffPassword);
        }
        staffInfo.setStaffCreateTime(new Date());

        int res = staffInfoService.insert(staffInfo);

        if(res > 0){
            return "redirect:/staff/all/list.do";
        }

        return "staff-add";
    }

    /*修改*/
    @RequestMapping("/staff/update.do")
    public String update(@RequestParam(value = "staffId") Integer staffId,
                         @RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffSex", required = false) Integer staffSex,
                         @RequestParam(value = "staffBirthday", required = false) Date staffBirthday,
                         @RequestParam(value = "staffDepartment", required = false) String staffDepartment,
                         @RequestParam(value = "staffPost", required = false) String staffPost,
                         @RequestParam(value = "staffIdCard", required = false) String staffIdCard,
                         @RequestParam(value = "staffIsWork", required = false) Integer staffIsWork,
                         @RequestParam(value = "staffPassword", required = false) String staffPassword,
                         HttpSession session){

        StaffInfo staffInfo = new StaffInfo();
        staffInfo.setStaffId(staffId);

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffInfo.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffInfo.setStaffName(staffName);
        }
        if(staffSex != null){
            staffInfo.setStaffSex(staffSex);
        }
        if(staffBirthday != null){
            staffInfo.setStaffBirthday(staffBirthday);
        }
        if(staffDepartment!=null && staffDepartment.trim().length()!=0){
            staffInfo.setStaffDepartment(staffDepartment);
        }
        if(staffPost!=null && staffPost.trim().length()!=0){
            staffInfo.setStaffPost(staffPost);
        }
        if(staffIdCard!=null && staffIdCard.trim().length()!=0){
            staffInfo.setStaffIdCard(staffIdCard);
        }
        if(staffIsWork!=null){
            staffInfo.setStaffIsWork(staffIsWork);
        }
        if(staffPassword!=null && staffPassword.trim().length()!=0){
            staffInfo.setStaffPassword(staffPassword);
        }

        int res = staffInfoService.updateByPrimaryKey(staffInfo);

        if(res > 0){

        }

        return "redirect:/staff/toEdit.do?staffId="+staffId+"";
    }


    /*删除单条(ajax)*/
    @RequestMapping("/staff/deleteOne.do")
    @ResponseBody
    public Object deleteOne(@RequestParam("staffId") Integer staffId){

        int res = staffInfoService.deleteByPrimaryKey(staffId);

        Map<String, String> map = new HashMap<String, String>();
        if(res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }

    /*跳转到新增页面*/
    @RequestMapping("/staff/toAdd.do")
    public String toAdd(Model model){
        List<BaseDict> sexList = baseDictService.findByDictTypeCode(TemplateParam.SEX_CODE);
        List<BaseDict> isWorkList = baseDictService.findByDictTypeCode(TemplateParam.IS_CODE);
        model.addAttribute("sexList", sexList);
        model.addAttribute("isWorkList", isWorkList);
        return "staff-add";
    }
    /*跳转到修改页面*/
    @RequestMapping("/staff/toEdit.do")
    public String toEdit(@RequestParam("staffId") Integer staffId, Model model){
        StaffInfo staffInfo = staffInfoService.findByPrimaryKey(staffId);
        model.addAttribute("staffInfo", staffInfo);

        List<BaseDict> sexList = baseDictService.findByDictTypeCode(TemplateParam.SEX_CODE);
        List<BaseDict> isWorkList = baseDictService.findByDictTypeCode(TemplateParam.IS_CODE);
        model.addAttribute("sexList", sexList);
        model.addAttribute("isWorkList", isWorkList);

        return "staff-edit";
    }
}
